﻿
/*
 * @file: ShippingDetailsDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for ShippingDetailsDA
/// </summary>
namespace DAL
{
    public class ShippingDetailsDA : BaseDAL, IShippingDetails
    {
        private const string tableName = "ShippingDetails";
        private const string orderID = "OrderID";
        private const string name = "Name";
        private const string address = "Address";
        private const string phone = "Phone";
        private const string mobilePhone = "MobilePhone";
        private const string note = "Note";
        private const string message = "Message";
        private const string deliveryDate = "DeliveryDate";

        public ShippingDetailsDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //#region IShippingDetails Members

        //public ShippingDetails GetShippingDetailsByOrdersID(int _orderID)
        //{
        //    ShippingDetails[] result = null;
        //    try
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandType = CommandType.Text;
        //        cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
        //            tableName, ordersID, ordersID);
        //        cmd.Parameters.Add(String.Format("@{0}", ordersID), SqlDbType.Int).Value = _orderID;

        //        String[] columnNames = {    ordersID,
        //                                    name,
        //                                    address,
        //                                    phone,
        //                                    mobilePhone,
        //                                    note,
        //                                    message,
        //                                    deliveryDate};
        //        result = SelectCollection<ShippingDetails>(columnNames, columnNames, cmd);

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    if (result.Length > 0)
        //        return result[0];
        //    else
        //        return null;
        //}

        //public int InsertShippingDetails(ShippingDetails _shippingDetails)
        //{
        //    int result = 0;
        //    try
        //    {
        //        String[] columnNames = {    ordersID,
        //                                    name,
        //                                    address,
        //                                    phone,
        //                                    mobilePhone,
        //                                    note,
        //                                    message,
        //                                    deliveryDate};
        //        Object[] values = { _shippingDetails.OrdersID,
        //                            _shippingDetails.Name,
        //                            _shippingDetails.Address,
        //                            _shippingDetails.Phone,
        //                            _shippingDetails.MobilePhone,
        //                            _shippingDetails.Note,
        //                            _shippingDetails.Message,
        //                            _shippingDetails.DeliveryDate};
        //        int autoNumber;
        //        result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;
        //}

        //#endregion

        #region IShippingDetails Members

        /*
         * @method: GetShippingByOrderID
         * @description: get a shippingdetails by order id
         * @input: orderID
         * @output: record corresponding
         */
        public ShippingDetails GetShippingByOrderID(int _orderID)
        {
            ShippingDetails[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                    tableName, orderID, orderID);
                cmd.Parameters.Add(String.Format("@{0}", orderID), SqlDbType.Int).Value = _orderID;


                String[] columnNames = {        orderID,
                                                name,
                                                address,
                                                phone,
                                                mobilePhone,
                                                note,
                                                message,
                                                deliveryDate};
                result = SelectCollection<ShippingDetails>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        ///*
        // * @method: GetShippingByBillID
        // * @description: get a shippingdetails by BillID
        // * @input: BillID, allowFilter <TRUE enables ignore hidden records>
        // * @output: record corresponding
        // */
        //public ShippingDetails GetShippingByBillID(int _billID, bool allowFilter)
        //{
        //    ShippingDetails[] result = null;
        //    try
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandType = CommandType.Text;
        //        if (allowFilter)
        //        {
        //            cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
        //                tableName, billID, billID, isEnable);
        //            cmd.Parameters.Add(String.Format("@{0}", billID), SqlDbType.Int).Value = _billID;
        //        }
        //        else {
        //            cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
        //                tableName, billID, billID);
        //            cmd.Parameters.Add(String.Format("@{0}", billID), SqlDbType.Int).Value = _billID;
        //        }

        //        String[] columnNames = {        shippingID,
        //                                        billID,
        //                                        userID,
        //                                        name,
        //                                        address,
        //                                        phone,
        //                                        mobilePhone,
        //                                        note,
        //                                        message,
        //                                        deliveryDate,
        //                                        isEnable};
        //        result = SelectCollection<ShippingDetails>(columnNames, columnNames, cmd);

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    if (result.Length > 0)
        //        return result[0];
        //    else
        //        return null;
        //}

        /*
         * @method: GetShippingByUserID
         * @description: get all shippingdetails by CustomerID
         * @input: CustomerID, allowFilter <TRUE enables ignore hidden records>
         * @output: all records corresponding
         */
        //public ShippingDetails[] GetShippingByUserID(int _userID, bool allowFilter)
        //{
        //    ShippingDetails[] result = null;
        //    try
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandType = CommandType.Text;
        //        if (allowFilter)
        //        {
        //            cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
        //                tableName, userID, userID, isEnable);
        //            cmd.Parameters.Add(String.Format("@{0}", userID), SqlDbType.Int).Value = _userID;
        //        }
        //        else {
        //            cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
        //               tableName, userID, userID);
        //            cmd.Parameters.Add(String.Format("@{0}", userID), SqlDbType.Int).Value = _userID;
        //        }

        //        String[] columnNames = {        shippingID,
        //                                        billID,
        //                                        userID,
        //                                        name,
        //                                        address,
        //                                        phone,
        //                                        mobilePhone,
        //                                        note,
        //                                        message,
        //                                        deliveryDate,
        //                                        isEnable};
        //        result = SelectCollection<ShippingDetails>(columnNames, columnNames, cmd);

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;
        //}

        /*
         * @method: InsertShippingDetails
         * @description: insert new ShippingDetails to database
         * @input: ShippingDetails entity
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int InsertShippingDetails(ShippingDetails _shippingDetails, out int autoNumber)
        {
            int result = 0;
            try
            {

                String[] columnNames = {        orderID,
                                                name,
                                                address,
                                                phone,
                                                mobilePhone,
                                                note,
                                                message,
                                                deliveryDate };

                Object[] values = { _shippingDetails.OrderID,
                                    _shippingDetails.Name,
                                    _shippingDetails.Address,
                                    _shippingDetails.Phone,
                                    _shippingDetails.MobilePhone,
                                    _shippingDetails.Note,
                                    _shippingDetails.Message,
                                    _shippingDetails.DeliveryDate};

                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: DisableShipping
         * @description: hidden a ShippingDetails
         * @input: shippingID, isEnable <FALSE if hidden>
         * @output: 1 if update successful, 0 if error occurred
         */
        //public int DisableShipping(int _shippingID, bool _isEnable) {
        //    int result = 0;
        //    try
        //    {
        //        String[] columnNames = { isEnable };
        //        Object[] values = { _isEnable };
        //        String[] keyColumnNames = { shippingID };
        //        Object[] keyColumnValues = { _shippingID };
        //        result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return result;
        //}

        #endregion
    }
}
